﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework
{
    public class DbHelper
    {
        private static readonly string connectionString = "Server=.;integrated security=SSPI;Initial Catalog=TestDB";

        public static SqlDataReader Query(string cmdString)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            using (SqlCommand myCom = new SqlCommand(cmdString, sqlConnection))
            {
                sqlConnection.Open();

                return myCom.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }

       
        internal static object GetDataTable(object cmdString)
        {
            throw new NotImplementedException();
        }

        public static int Operate(string cmdString)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            using (SqlCommand myCom = new SqlCommand(cmdString, sqlConnection))
            {
                sqlConnection.Open();

                return myCom.ExecuteNonQuery();
            }

        }


        public static DataTable GetDataTable(string cmdString)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);

            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, connectionString);

            DataTable table = new DataTable();
            dataAdapter.Fill(table);

            return table;
        }




    }
}
